<template>
  <el-dialog v-model="dialogVisible" class="!border-rd-10px" title="添加类型" width="700">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入类型名称" />
      </el-form-item>
      <el-form-item label="分类图标">
        <el-upload
          v-model:file-list="fileList"
          class="upload-demo"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          multiple
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :limit="3"
          :on-exceed="handleExceed"
        >
          <el-button type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="列表样式">
        <el-radio-group v-model="ruleForm.lbys">
          <el-radio value="1" size="large" border>一列一个</el-radio>
          <el-radio value="2" size="large" border>一列两个</el-radio>
          <el-radio value="3" size="large" border>一列三个</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否展示首页">
        <el-radio-group v-model="ruleForm.lbys">
          <el-radio value="1" size="large" border>展示</el-radio>
          <el-radio value="2" size="large" border>不展示</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

interface RuleForm {
  name: string
  school: null
  type: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '',
  school: [],
  type: ''
})

const rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, message: '请输入征订名称', trigger: 'blur' }],
  school: [
    {
      required: true,
      message: '请选择学校',
      trigger: 'change'
    }
  ],
  type: [
    {
      required: true,
      message: '请选择配送方式',
      trigger: 'change'
    }
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log('submit!')
    } else {
      // console.log('error submit!', fields)
    }
  })
}

const dialogVisible = ref(false)
function show(val) {
  dialogVisible.value = val
}
defineExpose({ show, dialogVisible })
</script>
<style scoped lang="scss"></style>
